package com.ucode.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ucode.crm.dictionary.CrmResultDefineMsg;
import com.ucode.crm.dictionary.ModuleType;
import com.ucode.crm.dictionary.TeamUserPower;
import com.ucode.crm.mapper.ContactsMapper;
import com.ucode.crm.mapper.FollowupTeamUserMapper;
import com.ucode.crm.mode.Contacts;
import com.ucode.crm.mode.FollowupTeamUser;
import com.ucode.crm.service.FollowupTeamUserService;
import com.ucode.tool.base.ResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.IdentifyUtils;
import cn.hutool.core.collection.CollUtil;

@Service
public class FollowupTeamUserServiceImpl implements FollowupTeamUserService {

    @Autowired
    private FollowupTeamUserMapper followTeamUserMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int batchInsert(Long targetId, Integer targetType, List<Long> userIds, Integer power) {
        if(CollUtil.isEmpty(userIds))
            return 0;
        
        List<FollowupTeamUser> followTeamUsers = new ArrayList<FollowupTeamUser>();
        Date now = new Date();
        for(Long userId : userIds){
            FollowupTeamUser followupTeamUser = new FollowupTeamUser(targetId, targetType, userId, power);
            followupTeamUser.setId(IdentifyUtils.getDefaultSnowflakeId());
            followupTeamUser.setCreatedTime(now);
            followupTeamUser.setUpdatedTime(now);
            followTeamUsers.add(followupTeamUser);
            
            if(ModuleType.customer.code == targetType){
                List<Contacts> contactsList = contactsMapper.findTeamSharedByCustomerId(targetId);
                if(CollUtil.isNotEmpty(contactsList)){
                    for(Contacts contacts : contactsList){
                        FollowupTeamUser ftu = new FollowupTeamUser(contacts.getId(), ModuleType.contacts.code, userId, power);
                        ftu.setId(IdentifyUtils.getDefaultSnowflakeId());
                        ftu.setCreatedTime(now);
                        ftu.setUpdatedTime(now);
                        followTeamUsers.add(ftu);
                    }
                }
            }
        }
        return followTeamUserMapper.batchSave(followTeamUsers);
    }

    @Override
    public int updatePower(Long id, Integer power) {
        int i = followTeamUserMapper.updatePower(id, power);
        if(i > 0){
            FollowupTeamUser followupTeamUser = followTeamUserMapper.findById(id);
            if(ModuleType.customer.code == followupTeamUser.getTargetType()){
                List<Contacts> contactsList = contactsMapper.findTeamSharedByCustomerId(followupTeamUser.getTargetId());
                if(CollUtil.isNotEmpty(contactsList)){
                    List<FollowupTeamUser> followTeamUsers = new ArrayList<FollowupTeamUser>();
                    Date now = new Date();
                    for(Contacts contacts : contactsList){
                        FollowupTeamUser ftu = new FollowupTeamUser(contacts.getId(), ModuleType.contacts.code, followupTeamUser.getUserId(), power);
                        ftu.setId(IdentifyUtils.getDefaultSnowflakeId());
                        ftu.setCreatedTime(now);
                        ftu.setUpdatedTime(now);
                        followTeamUsers.add(ftu);
                    }
                    followTeamUserMapper.batchSave(followTeamUsers);
                }
            }
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int delete(Long id,Long operatorId) {
        FollowupTeamUser followupTeamUser = followTeamUserMapper.findById(id);
        if(followupTeamUser == null)
            return 0;
        
        if(TeamUserPower.owner.code == followupTeamUser.getPower()){
            throw new UcodeServiceException(ResultCodeEnum.ILLEGAL_OPERATION.code,CrmResultDefineMsg.teamUser_owner_delete_error);
        }
        
        int i = followTeamUserMapper.delete(followupTeamUser.getId());
        if(i > 0){
            
            if(ModuleType.customer.code == followupTeamUser.getTargetType()){
                //同步联系人跟进团队
                List<Contacts> contactsList = contactsMapper.findListByCustomerId(followupTeamUser.getTargetId());
                if(CollUtil.isNotEmpty(contactsList)){
                    List<Long> delIds = new ArrayList<Long>();
                    for(Contacts contacts : contactsList){
                        FollowupTeamUser contacts_del_followupTeamUser = followTeamUserMapper.find(contacts.getId(), ModuleType.contacts.code, followupTeamUser.getUserId());
                        if(contacts_del_followupTeamUser == null)
                            continue;
                        delIds.add(contacts_del_followupTeamUser.getId());
                    }
                    if(CollUtil.isNotEmpty(delIds))
                        followTeamUserMapper.batchDelete(delIds);
                }
            }
        }
        
        return i;
    }

    @Override
    public int batchDelete(List<Long> ids) {
        return followTeamUserMapper.batchDelete(ids);
    }

    @Override
    public FollowupTeamUser findById(Long id) {
        return followTeamUserMapper.findById(id);
    }
    
    @Override
    public List<FollowupTeamUser> findByIds(List<Long> ids) {
        return followTeamUserMapper.findByIds(ids);
    }

    @Override
    public FollowupTeamUser find(Long targetId, Integer targetType, Long userId) {
        return followTeamUserMapper.find(targetId, targetType, userId);
    }

    @Override
    public List<FollowupTeamUser> findList(Long targetId, Integer targetType) {
        return followTeamUserMapper.findList(targetId, targetType);
    }
}
